package com.margo.project.socket;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.Socket;

/**
 * 线程处理
 * @author fangxingbang
 *
 */
public class ServiceHandleTask implements Runnable {

	private Socket socket;

	public ServiceHandleTask(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		InputStreamReader is=null;
		try {
			is= new InputStreamReader(socket.getInputStream(), "UTF-8");
			char[] c=new char[1024];
			
			int index;
			while ((index = is.read(c)) != -1) {
				System.out.println(new String(c, 0, index));
				//阻塞了
				break;
			}
//			// http请求
			Writer writer = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");			
			writer.write("HTTP/1.1 200\n");
			writer.write("Content-Type: application/json;charset=UTF-8\n");
			writer.write("Content-Length: 2\n");
			writer.write("Date: Wed, 07 Nov 2018 01:15:42 GMT\n");
			writer.write("\n");
			writer.write("ok\n");
			writer.write("\n");
			//会报错
			//writer.flush();
			writer.close();
			is.close();
			socket.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}

	}

}
